package org.utils.ui;

import java.awt.Component;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.ImageObserver;

class ImageTracker implements ImageObserver
{
    protected Image image;
    protected int width;
    protected int height;
    protected Component comp;
    protected boolean imageLoaded;
    protected boolean imageError;

    public ImageTracker(Image image1, Component component)
    {
        image = image1;
        comp = component;
    }

    public boolean gotError()
    {
        return imageError;
    }

    public synchronized boolean imageUpdate(Image image1, int i, int j, int k, int l, int i1)
    {
        if (!imageLoaded)
        {
            if((i & 0x1) != 0)
                this.width = l;
            if((i & 0x2) != 0)
                this.height = i1;
            if((i & 0x40) != 0)
            {
                imageError = true;
                imageLoaded = true;
                notify();
            } else
            if((i & 0x20) != 0)
            {
                imageLoaded = true;
                notify();
            }
        }
        return imageLoaded ^ true;
    }

    public synchronized void waitForImage()
    {
        boolean flag;
        if(comp != null)
            flag = comp.prepareImage(image, -1, -1, this);
        else
            flag = Toolkit.getDefaultToolkit().prepareImage(image, -1, -1, this);
        if(!flag)
            while(!imageLoaded) 
                try
                {
                    wait();
                }
                catch(InterruptedException _ex) { }
    }
}
